#include <stdio.h>

// O(n^2), like Bubble
void reverseString(char* s, int sSize) {
 #define swap(c1, c2) do{char _c = (c1); (c1) = (c2); (c2) = _c;} while(0)
    do {
        for (int i = 0; i < sSize-1; i++) {
            swap(*(s+i), *(s+i+1));
        }
    }while (sSize-->0);
}

// O(n)
void reverseString2(char* s, int sSize) {
 #define swap(c1, c2) do{char _c = (c1); (c1) = (c2); (c2) = _c;} while(0)
    int mid = sSize/2;
    for (int i = 0; i < mid; i++) {
        swap(*(s+i), *(s+sSize-i-1));
    }
}

// O(n)
void reverseString3(char* s, int sSize) {
 #define swap(c1, c2) do{char _c = (c1); (c1) = (c2); (c2) = _c;} while(0)
    for (int i = 0, j = sSize-1; i < j; i++, j--) {
        swap(*(s+i), *(s+j));
    }
}

int main(void)
{
    printf("#344. 反转字符串\n\n");
    char s[] = "abcd";
    printf("s %s\n", s);
    reverseString(s, sizeof(s)/sizeof(char)-1);
    printf("s %s\n", s);
    reverseString2(s, sizeof(s)/sizeof(char)-1);
    printf("s %s\n", s);
    reverseString3(s, sizeof(s)/sizeof(char)-1);
    printf("s %s\n", s);
    return 0;
}